<?php
// 新建投诉
class QuestionAction extends CommonAction {
    function _filter(&$map)
    {
        $map['number'] = array('like', "%" . $_POST['number'] . "%");
        $map['address'] = array('like', "%" . $_POST['address'] . "%");
        $map['isdel']=1;
        $map['postid'] = $_SESSION[C('USER_AUTH_KEY')];
        $map['isdel']=1;
    }
    public function index() {
        //列表过滤器，生成查询Map对象
        $map = $this->_search ();
        if (method_exists ( $this, '_filter' )) {
            $this->_filter ( $map );
        }
        $name=$this->getActionName();
        $model = D ($name);
        if (! empty ( $model )) {
            $this->_list ( $model, $map );
        }
        $this->display ();
        return;
    }
    protected function _list($model, $map, $sortBy = '', $asc = false, $countPk = 'id') {
        //排序字段 默认为主键名
        if (!empty ( $_REQUEST ['_order'] )) {
            $order = $_REQUEST ['_order'];
        } else {
            $order = ! empty ( $sortBy ) ? $sortBy : $model->getPk ();
        }
        //排序方式默认按照倒序排列
        //接受 sost参数 0 表示倒序 非0都 表示正序
        if (isset ( $_REQUEST ['_sort'] )) {
//			$sort = $_REQUEST ['_sort'] ? 'asc' : 'desc';
            $sort = $_REQUEST ['_sort'] == 'asc' ? 'asc' : 'desc'; //zhanghuihua@msn.com
        } else {
            $sort = $asc ? 'asc' : 'desc';
        }
        //取得满足条件的记录数
        $count = $model->where ( $map )->count ( $countPk );
        if ($count > 0) {
            import ( "@.ORG.Util.Page" );
            //创建分页对象
            if (! empty ( $_REQUEST ['listRows'] )) {
                $listRows = $_REQUEST ['listRows'];
            } else {
                $listRows = '';
            }
            $p = new Page ( $count, $listRows );
            //分页查询数据
            $voList = $model->where($map)->order( "`" . $order . "` " . $sort)->limit($p->firstRow . ',' . $p->listRows)->select ( );

            //echo $model->getlastsql();
            //分页跳转的时候保证查询条件
            foreach ( $map as $key => $val ) {
                if (! is_array ( $val )) {
                    $p->parameter .= "$key=" . urlencode ( $val ) . "&";
                }
            }
            //分页显示
            $page = $p->show ();
            //列表排序显示
            $sortImg = $sort; //排序图标
            $sortAlt = $sort == 'desc' ? '升序排列' : '倒序排列'; //排序提示
            $sort = $sort == 'desc' ? 1 : 0; //排序方式

            $reason_list=C(QUESTIONREASON);
            $this->assign ( 'reason_list', $reason_list );
            $status_list=C(STATUS);
            $this->assign ( 'status_list', $status_list );
            $city_list = C(CITY);
            $this->assign('city_list', $city_list);
            $jiaose_list = C(JIAOSE);
            $this->assign('jiaose_list', $jiaose_list);
            //模板赋值显示
            $this->assign ( 'list', $voList );
            $this->assign ( 'sort', $sort );
            $this->assign ( 'order', $order );
            $this->assign ( 'sortImg', $sortImg );
            $this->assign ( 'sortType', $sortAlt );
            $this->assign ( "page", $page );
        }
        $this->assign ( 'totalCount', $count );
        $this->assign ( 'numPerPage', $p->listRows );
        $this->assign ( 'currentPage', !empty($_REQUEST[C('VAR_PAGE')])?$_REQUEST[C('VAR_PAGE')]:1);

        Cookie::set ( '_currentUrl_', __SELF__ );
        return;
    }


    public function _before_add(){
        $Group = D('Role');
        //查找满足条件的列表数据
        $reason_list=C(QUESTIONREASON);
        $this->assign('reason_list',$reason_list);

    }

    function read() {
        $name=$this->getActionName();
        $model = M ( $name );
        $vo = $model->getById ( $_GET[pid] );
        $reason_list=C(QUESTIONREASON);
        $this->assign('reason_list',$reason_list);
        $status_list=C(STATUS);
        $city_list = C(CITY);
        $po= M("Process")->where('questionid='.$_GET[pid])->order('reply_time')->select();
        $numbercount=$model->where('number = '.$_GET[pnumber] .' and isdel=1')->count();
        $this->assign('numbercount', $numbercount);
        $this->assign ( 'polist', $po );
        $this->assign('city_list', $city_list);
        $this->assign('status_list',$status_list);
        $this->assign ( 'vo', $vo );
        $this->display ();
    }


    function insert() {

        import('ORG.Net.UploadFile');
        $upload = new UploadFile();
        $upload->maxSize  = 2097152 ;// 限制2m
        $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg','bmp');// 设置图片上传类型
        $upload->saveRule=time;
        $upload->savePath =  '../Public/questionupload/';// 设置附件上传目录
        if(!$upload->upload()) {// 上传错误提示错误信息
            if(isset($uploadFileInfo)){
                $this->error($upload->getErrorMsg());
            }
        }else{// 上传成功 获取上传文件信息
            $info =  $upload->getUploadFileInfo();

        }

        $name=$this->getActionName();
        $model = D ($name);
        if (false === $model->create ()) {
            $this->error ( $model->getError () );
        }
        //保存当前数据对象
        $list=$model->add ();
        if ($list!==false) { //保存成功
            $uppic['pic']=$info[0]['savename'];
            $this->update($list,$uppic);
            $this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
            $this->success ('新增成功!');

        } else {
            //失败提示
            $this->error ('新增失败!');
        }
    }


    function update($id,$updata) {
        // 更新数据
        $name=$this->getActionName();
        $model=D($name);
        $model->where("id = " . $id)->data($updata)->save();
    }

    function updateremark() {
        // 更新数据
        $updata['remark']=$_POST['remark'];
        $id=$_POST['id'];
        $name=$this->getActionName();
        $model=D($name);
        $model->where("id = " . $id)->data($updata)->save();
        $this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
        $this->success ('编辑成功!');
    }

}
?>